﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Web.SessionState;
using SES.CMS.DO;
using SES.CMS.BL;

namespace SES.CMS
{
    public partial class Search2 : System.Web.UI.Page
    {
        ShopingCart Cart = new ShopingCart();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                load();
            }
        }
        public void load()
        {
            if (Request.QueryString["ID"] != null)
            {
                DataTable dt = new cmsSanPhamBL().TimKiem(Request.QueryString["ID"].ToString());
                bindatalist(dt);
                lblInfo.Text = "Có " + dt.Rows.Count + " sản phẩm có tên <a href='/Search/" + Request.QueryString["ID"].ToString() + "/Tim-Kiem.aspx'><span style='color:red;font-weight:bold;'>" + Request.QueryString["ID"].ToString() + "</span></a>";
                Page.Title = Request.QueryString["ID"].ToString() + " - FriendShop";
            }
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }
        private void bindatalist(DataTable dt)
        {
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string url = Request.Url.AbsolutePath;
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);
                long ClientLoginID = 0;
                if (Session["IDDN"] != null)
                    ClientLoginID = long.Parse(Session["IDDN"].ToString());
                DataTable tb = new DataTable();
                if (Session["GioHang"] != null)
                {
                    Cart.setTable((DataTable)Session["GioHang"]);
                    tb = Cart.getTable();
                    bool kt = false;
                    foreach (DataRow dr in tb.Rows)
                    {
                        if (Int64.Parse(dr[1].ToString()) == id)
                            kt = true;
                    }
                    if (kt == true)
                        tb = Cart.upDate(tb, id, 1, gia);
                    else
                        tb = Cart.addSP(id, 1, gia);
                    Session["GioHang"] = tb;
                }
                else
                {
                    tb = Cart.init(id, 1, gia);
                    Session["GioHang"] = tb;
                }

                Ultility.Alert("Đặt mua thành công!" + tb.Rows.Count, url);
            }
            catch
            {
                Ultility.Alert("Lỗi!", url);
            }
        }
    }
}
